package com.dacong.nezha.utils;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class Xiadan {


    /**
     * 购买店铺
     * @param num
     * @param token
     * @return
     */
    public static String buySlink(Integer num,String token){
        String url = "/slinkUser/purchase";
        String method = "POST";
        String param = "{\"id\":\"07fc9e0a8eaa4b3b94efea38a698a768\",\"purchaseCount\":"+num+"}";
        Map<String, Object> body = Request.createBody(url, method, param, token);
        return ExcuteHttp.executeHttp(JSONUtil.toJsonStr(body));

    }

    public static String slinkList(String token){
        String url = "/slink/list";
        String method = "POST";
        String param = "{\"page\":0,\"size\":100,\"status\":\"3\"}";
        Map<String, Object> body = Request.createBody(url, method, param, token);
        return ExcuteHttp.executeHttp(JSONUtil.toJsonStr(body));
    }

    /**
     * 查询复活列表
     * @param token
     * @return
     */
    public static String slinkListFuhuo(String token){
        String url = "/slink/list";
        String method = "POST";
        String param = "{\"page\":0,\"size\":100,\"status\":\"5\"}";
        Map<String, Object> body = Request.createBody(url, method, param, token);
        return ExcuteHttp.executeHttp(JSONUtil.toJsonStr(body));
    }

    /**
     * 更换店铺连接
     * @param slinkId
     * @param token
     * @return
     */
    public static String changeSlink(String slinkId,String token){
        String url = "/slink/changeSlink";
        String method = "POST";
        String param = "{\"slinkId\":\""+slinkId+"\"}";
        Map<String, Object> body = Request.createBody(url, method, param, token);
        return ExcuteHttp.executeHttp(JSONUtil.toJsonStr(body));
    }

    /**
     * 获取下单收货地址
     * @param slinkId
     * @param token
     * @return
     */
    public static Map<String,String> slinkGetRecipient(String slinkId,String token){
        String url = "/slink/getRecipient";
        String method = "POST";
        String param = "{\"slinkId\":\""+slinkId+"\"}";
        Map<String, Object> body = Request.createBody(url, method, param, token);
        Map<String,String> recipient = new HashMap<>();
        String s = ExcuteHttp.executeHttp(JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(s);
        JSONObject data = entries.getJSONObject("data");
        recipient.put("city",data.getStr("city"));
        recipient.put("district",data.getStr("district"));
        recipient.put("postalCode",data.getStr("postalCode"));
        recipient.put("province",data.getStr("province"));
        recipient.put("recipientMobile",data.getStr("recipientMobile"));
        recipient.put("recipientName",data.getStr("recipientName"));
        recipient.put("regionsAddress",data.getStr("regionsAddress"));
        return recipient;
    }

    /**
     * 准备下单
     * @param ckId
     * @param step
     * @param token
     * @return
     */
    public static String prepareMallOrder(String ckId,Integer step,String token){
        String url = "/ck/prepareMallOrder";
        String method = "POST";
        String param = "{\"ckId\":\""+ckId+"\",\"step\":"+step+"}";
        Map<String, Object> body = Request.createBody(url, method, param, token);

        String s = ExcuteHttp.executeHttp(JSONUtil.toJsonStr(body));
        JSONObject entries = JSONUtil.parseObj(s);
        String data = entries.getStr("data");
        JSONObject dataObj = JSONUtil.parseObj(data);
        String liveMsg = dataObj.getStr("liveMsg");
        return s;
    }

    /**
     * 实际下单
     * @param slinkId
     * @param ckId
     * @param recipient
     * @param token
     * @return
     */
    public static String createMallOrder(String slinkId,String ckId,Map<String,String> recipient,String token){
        String params = "{\n" +
                "    \"url\": \"/slink/createMallOrder\",\n" +
                "    \"options\": {\n" +
                "        \"method\": \"POST\",\n" +
                "        \"body\": \"{\\\"no_captcha\\\":true,\\\"slinkId\\\":\\\""+slinkId+"\\\",\\\"ckIds\\\":[\\\""+ckId+"\\\"],\\\"minPrice\\\":30,\\\"maxPrice\\\":40,\\\"province\\\":\\\""+recipient.get("province")+"\\\",\\\"city\\\":\\\""+recipient.get("city")+"\\\",\\\"district\\\":\\\""+recipient.get("district")+"\\\",\\\"regionsAddress\\\":\\\""+recipient.get("regionsAddress")+"\\\",\\\"recipientName\\\":\\\""+recipient.get("recipientName")+"\\\",\\\"recipientMobile\\\":\\\""+recipient.get("recipientMobile")+"\\\",\\\"postalCode\\\":\\\""+recipient.get("postalCode")+"\\\"}\",\n" +
                "        \"headers\": {\n" +
                "            \"Content-Type\": \"application/json\",\n" +
                "            \"Authorization\": \""+token+"\",\n" +
                "            \"Language\": \"zh-CN\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        return ExcuteHttp.executeHttp(params);
    }



}
